(function($) {
	$.fn.pager = function(options) {
		
		var defaultOptions = {
			'object' : 'User',
			'divTable' : 'pagerTable',
			'divPager' : 'pager',
			'divMainPager' : 'divMainPager',
			'nbrRows' : 10,
			'jspPage' : 'listPagerObject.jsp',
			'currentPage' : 0,
			onChangePage : null,
			onComplete : null,
			'position' : 'both',
			'getTable' : true,
			'jspPagePath': 'pages/machine/',
			'modelPackage' : 'ma.mindware.mpm.domain',
			'tools' : {'print':false,'refresh':false},
			'divDetail': 'divDetail'
		};
		
		var plugin = this;
		var contener = $(this);

		if (options) {
			$.extend(defaultOptions, options);
		}
		
		
		plugin.init = function(){
			plugin.createDiv();
			plugin.getPager();
			return $("#"+defaultOptions.divMainPager);
		};
		plugin.hide = function(){
			$("#pagerDiv").hide();
		};
		plugin.show = function(){
			$("#pagerDiv").show();
		};
		plugin.displayAjaxLoader = function(){
			$("#"+defaultOptions.divTable).html("&nbsp;");
			$("#"+defaultOptions.divTable).addClass('pager-ajaxLoader');
		};
		plugin.getTable = function(){
			
			$.ajax({
				url : 'getTable',
				type : 'post',
				async : false,
				data : {
					jsonPagerObject : JSON.stringify(defaultOptions)
				},
				dataType : 'html',
				success : function(resp) {
					plugin.displayAjaxLoader();
					$("#"+defaultOptions.divTable).hide();
					$("#"+defaultOptions.divTable).html(resp);
					$("#"+defaultOptions.divTable).show();
					$("#"+defaultOptions.divTable).removeClass('pager-ajaxLoader');
				},
				error : function(resp) {
					zebraNotification("Erreur ajax request Table",3);
				}
			});
		};
		
		plugin.getPager = function(){
			
			if(defaultOptions.getTable)	plugin.getTable();
			$.ajax({
				url : 'getPager',
				type : 'post',
				async : false,
				data : {
					jsonPagerObject : JSON.stringify(defaultOptions)
				},
				dataType : 'json',
				success : function(resp) {
					jQuery.ajaxSetup({	async : false });
				if(resp.nbrPage!=0 && resp.nbrPage>1){
					$("#currentPage").val(defaultOptions.currentPage);
					$("#"+defaultOptions.divPager+"_top").html("<table align='center'><tr>" +
							"<td id='firstPageTd_top' ><span id='top_firstPageLink' class='firstPage'></span></td>" +
							"<td id='previousTdLink_top' ><span id='previousPagelink_top' class='previousPage'></span></td>" +
							"<td id='selectTd_top'><input class='inputIndex' type='text' id='currentPage_top' name='currentPage_top' value='1'  /><input class='inputIndex' id='nbrPage_top' value='"+resp.nbrPage+"' disabled />"+
							"<select id='indexPage_top' style='display : none;'></select></td>" +
							"<td id='nextTdLink_top' ><span id='nextPagerLink_top' class='nextPage'></span></td>" +
							"<td id='lastPageTd_top' ><span id='lastPageLink_top' class='lastPage'></span></td>" +
							"<td id='toolsPrintTd_top' ><span id='pagerPrint_top' class='toolsPrint'></span></td>" +
							"<td id='toolsRefreshTd_top' ><span id='pagerRefresh_top' class='toolsRefresh'></span></td>" +
							"</tr></table>");
					
					$("#"+defaultOptions.divPager+"_bottom").html("<table align='center'><tr>" +
							"<td id='firstPageTd_bottom' ><span  id='bottom_firstPageLink' class='firstPage'></span></td>" +
							"<td id='previousTdLink_bottom' ><span id='previousPageLink_bottom' class='previousPage'></span></td>" +
							"<td id='selectTd_bottom'><input class='inputIndex' type='text' id='currentPage_bottom' name='currentPage_bottom' value='1' /><input class='inputIndex' id='nbrPage_bottom' value='"+resp.nbrPage+"' disabled />"+
							"<select id='indexPage_bottom' style='display : none;'></select></td>" +
							"<td id='nextTdLink_bottom' ><span  id='nextPagerLink_bottom' class='nextPage'></span></td>" +
							"<td id='lastPageTd_bottom' ><span id='lastPageLink_bottom' class='lastPage'></span></td>" +
							"</tr></table>");
					
					if(defaultOptions.tools.print) $("#pagerPrint_top").css({'display':'inline-table'});
					if(defaultOptions.tools.refresh) $("#pagerRefresh_top").css({'display':'inline-table'});
					
					
					for(var i=1;i<=resp.nbrPage;i++){
						$("#indexPage_top,#indexPage_bottom").append("<option value='"+i+"'>"+i+"</option>");
					}
					
					if(parseInt(resp.nbrPage)<=1){
						$("#previousTdLink_top").hide();
						$("#nextTdLink_top").hide();
						$("#lastPageTd_top").hide();
						$("#firstPageTd_top").hide();
						
						$("#previousTdLink_bottom").hide();
						$("#nextTdLink_bottom").hide();
						$("#lastPageTd_bottom").hide();
						$("#firstPageTd_bottom").hide();
						
					}else {
						$("#previousTdLink_top").hide();
						$("#firstPageTd_top").hide();
						
						$("#previousTdLink_bottom").hide();
						$("#firstPageTd_bottom").hide();
					}
					
					$("#previousPagelink_top,#previousPageLink_bottom").click(function(){
						plugin.getPreviousPage(resp);
						if (typeof defaultOptions.onChangePage == "function") {
							defaultOptions.onChangePage();
							defaultOptions.onComplete();
						}
					});
					$("#nextPagerLink_top,#nextPagerLink_bottom").click(function(){
						console.log("click");
						plugin.getNextPage(resp);
						if (typeof defaultOptions.onChangePage == "function") {
							defaultOptions.onChangePage();
							defaultOptions.onComplete();
						}
					});
					$("#top_firstPageLink,#bottom_firstPageLink").click(function(){
						defaultOptions.currentPage=parseInt($("#indexPage_top option:first").val())-1;
						plugin.getSelectPage(resp);
						if (typeof defaultOptions.onChangePage == "function") {
							defaultOptions.onChangePage();
							defaultOptions.onComplete();
						}
					});
					$("span[id='lastPageLink_top'],span[id='lastPageLink_bottom']").click(function(){
						defaultOptions.currentPage=parseInt($("#indexPage_top option:last").val())-1;
						plugin.getSelectPage(resp);
						if (typeof defaultOptions.onChangePage == "function") {
							defaultOptions.onChangePage();
							defaultOptions.onComplete();
						}
					});
					$("#indexPage_top,#indexPage_bottom").change(function(){
						defaultOptions.currentPage=parseInt($(this).val())-1;
						plugin.getSelectPage(resp);
						if (typeof defaultOptions.onChangePage == "function") {
							defaultOptions.onChangePage();
							defaultOptions.onComplete();
						}
					});
					$("#currentPage_top,#currentPage_bottom").change(function(){
						var lastIndexPage=parseInt($("#indexPage_top option:last").val());
						//console.log("last "+lastIndexPage+" val"+ $(this).val());
						if(parseInt($(this).val())<=lastIndexPage && parseInt($(this).val())>0 ){
							defaultOptions.currentPage=parseInt($(this).val())-1;
							plugin.getSelectPage(resp);
							if (typeof defaultOptions.onChangePage == "function") {
								defaultOptions.onChangePage();
							}
						}else{
							$("#currentPage_top,#currentPage_bottom").val(defaultOptions.currentPage+1);
							//$("#currentPage_bottom").val(defaultOptions.currentPage+1);
							zebraNotification("Page inexistante !",3);
						}
					});
					jQuery.ajaxSetup({	async : true });
				}else if(resp.nbrPage!=0 && resp.nbrPage==1){
					
				}else{
					zebraNotification("Liste vide !",3);
				}
				if (typeof defaultOptions.onComplete == "function") {
					defaultOptions.onComplete();	
				}
				},
				error : function(resp) {
					zebraNotification("Erreur ajax request ",3);
				}
			});
		};
		
		plugin.testDirection = function(resp,direction){
			$("#indexPage_top,#indexPage_bottom").val(defaultOptions.currentPage+1);
			if(direction=='right' && defaultOptions.currentPage >= (resp.nbrPage-1) && defaultOptions.currentPage>0){
				$("#previousTdLink_top").show();
				$("#firstPageTd_top").show();
				$("#nextTdLink_top").hide();
				$("#lastPageTd_top").hide();
				
				$("#previousTdLink_bottom").show();
				$("#firstPageTd_bottom").show();
				$("#nextTdLink_bottom").hide();
				$("#lastPageTd_bottom").hide();
				
			}
			if(direction=='right' && defaultOptions.currentPage < (resp.nbrPage-1) && defaultOptions.currentPage>0){
				$("#previousTdLink_top").show();
				$("#nextTdLink_top").show();
				$("#firstPageTd_top").show();
				$("#lastPageTd_top").show();
				
				$("#previousTdLink_bottom").show();
				$("#nextTdLink_bottom").show();
				$("#firstPageTd_bottom").show();
				$("#lastPageTd_bottom").show();
			}
			if(direction=='left' && defaultOptions.currentPage <= 0 && defaultOptions.currentPage < (resp.nbrPage-1)){
				$("#previousTdLink_top").hide();
				$("#nextTdLink_top").show();
				$("#firstPageTd_top").hide();
				$("#lastPageTd_top").show();
				
				$("#previousTdLink_bottom").hide();
				$("#nextTdLink_bottom").show();
				$("#firstPageTd_bottom").hide();
				$("#lastPageTd_bottom").show();
			}
			if(direction=='left' && defaultOptions.currentPage < (resp.nbrPage-1) && defaultOptions.currentPage>0){
				$("#previousTdLink_top").show();
				$("#nextTdLink_top").show();
				$("#firstPageTd_top").show();
				$("#lastPageTd_top").show();
				
				$("#previousTdLink_bottom").show();
				$("#nextTdLink_bottom").show();
				$("#firstPageTd_bottom").show();
				$("#lastPageTd_bottom").show();
			}
		};
		
		plugin.getNextPage = function(resp){
			defaultOptions.currentPage=defaultOptions.currentPage+1;
			$("#currentPage_top,#currentPage_bottom").val(defaultOptions.currentPage+1);
			//$("#currentPage_bottom").val(defaultOptions.currentPage+1);
			if(defaultOptions.getTable) plugin.getTable();
			plugin.testDirection(resp,'right');
			
		};
		plugin.getPreviousPage = function(resp){
			defaultOptions.currentPage=defaultOptions.currentPage-1;
			$("#currentPage_top,#currentPage_bottom").val(defaultOptions.currentPage+1);
			//$("#currentPage_bottom").val(defaultOptions.currentPage+1);
			if(defaultOptions.getTable) plugin.getTable();
			plugin.testDirection(resp,'left');
		};
		
		plugin.getSelectPage = function(resp){
			
			if(defaultOptions.getTable) plugin.getTable();
			if(parseInt($("#currentPage_top").val())>defaultOptions.currentPage){
				plugin.testDirection(resp,'left');
			}else{
				plugin.testDirection(resp,'right');
			}
			
			$("#currentPage_top,#currentPage_bottom").val(defaultOptions.currentPage+1);
			//$("#currentPage_bottom").val(defaultOptions.currentPage+1);
		};
		plugin.createDiv = function(){
			contener.html("<div id='"+defaultOptions.divMainPager+"' ><div class='pagerDiv' id='"+defaultOptions.divPager+"_top'></div>" +
					"<div class='pagerTableDiv' id='"+defaultOptions.divTable+"' ></div>" +
							"<div class='pagerDiv' id='"+defaultOptions.divPager+"_bottom'></div></div><div id='"+defaultOptions.divDetail+"'></div>");
			if(defaultOptions.position=='top')
				$("#"+defaultOptions.divPager+"_bottom").hide();
			else if(defaultOptions.position=='bottom')
				$("#"+defaultOptions.divPager+"_top").hide();
		};
		
		return plugin.init();

	};
})(jQuery);